package com.secsys.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.secsys.dto.ResourceDTO;
import com.secsys.dto.RoleDTO;
import com.secsys.model.TbSysLog;
import com.secsys.model.TbSysResource;
import com.secsys.model.TbSysRole;
import com.secsys.model.TbSysRoleResource;
import com.secsys.model.TbSysUser;
import com.secsys.service.impl.LogServiceImpl;
import com.secsys.service.impl.ResourceServiceImpl;
import com.secsys.service.impl.RoleResourceServiceImpl;
import com.secsys.service.impl.RoleServiceImpl;

/**
 * @Description 角色控制类
 * @author LiusQ
 *
 */
public class RoleAction extends BaseAction<RoleDTO> {
	
	private RoleServiceImpl roleService;
	
	private ResourceServiceImpl resourceService;
	
	private RoleResourceServiceImpl roleResourceService;
	
	private LogServiceImpl logService;
	
	private List<RoleDTO> roleList;
	
	private List<TbSysRoleResource> roleResourceList;
	
	private List<ResourceDTO> resourceDtoList;
	
	private RoleDTO roleDto;
	
	private Integer roleId;
	
	private Integer roleResourceId;
	
	private Integer resourceId;
	
	@Override
	public RoleDTO getModel() {
		// TODO Auto-generated method stub
		if(roleDto==null){
			roleDto=new RoleDTO();
		}
		return roleDto;
	}
	
	/**
	 * 添加角色,并添加工作日志
	 * @return
	 */
	public String addRole(){

		roleService.addRole(roleService.getBeanFromDto(roleDto));
		TbSysLog sysLog = new TbSysLog();
		sysLog.setOperation("添加" + roleDto.getRole() + " 角色。" );
		TbSysUser userDto = (TbSysUser)session.get("userDto");
		sysLog.setOperationer(userDto.getUserName());
		Date opTime = new Date(); 
		sysLog.setOpTime(opTime);
		String remark = roleService.findTbSysRoleByName(roleDto.getRole()).getId().toString();
		sysLog.setRemark(remark);
		sysLog.setLogFlag(0);
		List<String> operationList = new ArrayList<String>();
		operationList.add("添加" + roleDto.getRole() + " 角色。" );
		operationList.add("修改" + roleDto.getRole() + " 角色。" );
		//operationList.add("删除" + roleDto.getRole() + " 角色。" );
		List<TbSysLog> logTest = logService.listInOperation(operationList);
		if(logTest!=null && logTest.size()>0){
			for(TbSysLog sysLogTest : logTest){
				sysLogTest.setLogFlag(3);
				logService.saveOrUpdate(sysLogTest);
			}
		}
		logService.addLog(sysLog);//插入日志
		request.put("successMsg", roleDto.getRole() +" 角色已添加成功!!!");
		request.put("successUrl", "/OneSecSys/bbs/role/role_listRole.action");
		return "success";
	}
	
	/**
	 * 查找该角色所拥有的资源权限
	 * @return
	 */
	public String showResource(){
		
		roleResourceList = roleResourceService.queryByRoleId(roleId);
		roleDto = roleService.getDtoFromPojo(roleService.findById(roleId));
		request.put("roleResourceMsg", "拥有的");
		return "showResource";
		
	}
	
	/**
	 * 查找该角色未添加的资源
	 * @return
	 */
	public String notOwnResource(){
		roleResourceList = roleResourceService.queryByRoleId(roleId);
		if(roleResourceList != null && roleResourceList.size()>0 ){
			List<Integer> resourceIdList = new ArrayList<Integer>();
			for(TbSysRoleResource roleResource : roleResourceList){
				resourceIdList.add(roleResource.getTbSysResource().getId());
			}
			resourceDtoList = resourceService.listNotInResource(resourceIdList);
		}else{
			resourceDtoList = resourceService.listResource();
		}
		
		roleDto = roleService.getDtoFromPojo(roleService.findById(roleId));
		request.put("roleResourceMsg", "未添加的");
		return "notOwnResource";
	}
	
	/**
	 * 查找角色列表
	 * @return
	 */
	public String listRole(){
		roleList = roleService.listRole();
		return "listRole";
	}
	
	/**
	 * 删除角色的资源，并添加日志
	 * @return
	 */
	public String roleResourceDelete(){
		
		
		request.put("successMsg", "该角色的资源已删除!!!");
		request.put("successUrl", "/OneSecSys/bbs/role/role_listRole.action");
		TbSysLog sysLog = new TbSysLog();
		TbSysRoleResource roleResourceLog = roleResourceService.findById(roleResourceId);
		sysLog.setOperation("删除" + roleResourceLog.getTbSysRole().getRole() + " 角色的"  + roleResourceLog.getTbSysResource().getMenuName() +" 资源" );
		TbSysUser userDto = (TbSysUser)session.get("userDto");
		sysLog.setOperationer(userDto.getUserName());
		Date opTime = new Date(); 
		sysLog.setOpTime(opTime);
		String remark = roleResourceLog.getTbSysResource().getId().toString() + "," + roleResourceLog.getTbSysRole().getId().toString();
		sysLog.setRemark(remark);
		sysLog.setLogFlag(0);
		List<String> operationList = new ArrayList<String>();
		operationList.add("删除" + roleResourceLog.getTbSysRole().getRole() + " 角色的"  + roleResourceLog.getTbSysResource().getMenuName() +" 资源" );
		operationList.add("为" + roleResourceLog.getTbSysRole().getRole() + " 角色添加"  + roleResourceLog.getTbSysResource().getMenuName() +" 资源。" );
		//operationList.add("删除" + roleDto.getRole() + " 角色。" );
		List<TbSysLog> logTest = logService.listInOperation(operationList);
		if(logTest!=null && logTest.size()>0){
			for(TbSysLog sysLogTest : logTest){
				sysLogTest.setLogFlag(3);
				logService.saveOrUpdate(sysLogTest);
			}
		}
		logService.addLog(sysLog);//插入日志
		roleResourceService.deleteById(roleResourceId);
		return "roleResourceDelete";
	}
	
	/**
	 * 为该角色添加资源,并添加日志
	 * @return
	 */
	public String roleResourceAdd(){
		TbSysRoleResource roleResourceAdd = new TbSysRoleResource();
		roleResourceAdd.setTbSysRole(roleService.findById(roleId));
		roleResourceAdd.setTbSysResource(resourceService.findById(resourceId));
		roleResourceService.save(roleResourceAdd);
		TbSysLog sysLog = new TbSysLog();
		sysLog.setOperation("为" + roleResourceAdd.getTbSysRole().getRole() + " 角色添加"  + roleResourceAdd.getTbSysResource().getMenuName() +" 资源。" );
		TbSysUser userDto = (TbSysUser)session.get("userDto");
		sysLog.setOperationer(userDto.getUserName());
		Date opTime = new Date(); 
		sysLog.setOpTime(opTime);
		String remark = roleResourceService.findRoleResourceByRoleIdAndResourceId(roleId, resourceId).getId().toString();
		sysLog.setRemark(remark);
		sysLog.setLogFlag(0);
		List<String> operationList = new ArrayList<String>();
		operationList.add("删除" + roleResourceAdd.getTbSysRole().getRole() + " 角色的"  + roleResourceAdd.getTbSysResource().getMenuName() +" 资源" );
		operationList.add("为" + roleResourceAdd.getTbSysRole().getRole() + " 角色添加"  + roleResourceAdd.getTbSysResource().getMenuName() +" 资源。" );
		//operationList.add("删除" + roleDto.getRole() + " 角色。" );
		List<TbSysLog> logTest = logService.listInOperation(operationList);
		if(logTest!=null && logTest.size()>0){
			for(TbSysLog sysLogTest : logTest){
				sysLogTest.setLogFlag(3);
				logService.saveOrUpdate(sysLogTest);
			}
		}
		logService.addLog(sysLog);//插入日志
		request.put("successMsg", "资源已成功添加到该角色!!!");
		request.put("successUrl", "/OneSecSys/bbs/role/role_listRole.action");
		return "roleResourceAdd";
	}
	
	/**
	 * 创建修改角色界面
	 * @return
	 */
	public String updateRoleUI(){
		
		roleDto = roleService.getDtoFromPojo(roleService.findById(roleId));
		return "updateRoleUI";
	}
	
	/**
	 * 修改角色,并添加工作日志
	 * @return
	 */
	public String updateRole(){
		TbSysRole roleLog = roleService.findById(roleDto.getId());
		TbSysLog sysLog = new TbSysLog();
		sysLog.setOperation("修改" + roleDto.getRole() + " 角色。" );
		TbSysUser userDto = (TbSysUser)session.get("userDto");
		sysLog.setOperationer(userDto.getUserName());
		Date opTime = new Date(); 
		sysLog.setOpTime(opTime);
		String remark = roleLog.getId().toString()+"," +roleLog.getRole()+","+roleLog.getStatue()+","+roleLog.getRemark();
		sysLog.setRemark(remark);
		sysLog.setLogFlag(0);
		List<String> operationList = new ArrayList<String>();
		operationList.add("添加" + roleDto.getRole() + " 角色。" );
		operationList.add("修改" + roleDto.getRole() + " 角色。" );
		//operationList.add("删除" + roleDto.getRole() + " 角色。" );
		List<TbSysLog> logTest = logService.listInOperation(operationList);
		if(logTest!=null && logTest.size()>0){
			for(TbSysLog sysLogTest : logTest){
				sysLogTest.setLogFlag(3);
				logService.saveOrUpdate(sysLogTest);
			}
		}
		logService.addLog(sysLog);//插入日志
		roleService.saveOrUpdate(roleService.getBeanFromDto(roleDto));
		request.put("successMsg", roleDto.getRole()+ " 角色已修改成功!!!");
		request.put("successUrl", "/OneSecSys/bbs/role/role_listRole.action");
		return "updateRoleSuccess";
	}
	
	/**
	 * 检查角色名是否已存在
	 * @return
	 */
	public String validateData()throws Exception{
		String roleName = new String(roleDto.getRole().getBytes("iso8859-1"),"gbk");
		System.out.println("***********************"+ roleName);
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter(); 
		StringBuffer validateMsg=null;
		if("".equals(roleName)){
			validateMsg = new StringBuffer("");
		}else{
			TbSysRole roleTest = roleService.findTbSysRoleByName(roleName);
			if(roleTest==null){	
				validateMsg = new StringBuffer("<font color='green'>ok</font>");		
			}else{
				validateMsg = new StringBuffer("<font color='red'> 角色名已存在</font>");			
			}
		}
		
		out.print(validateMsg.toString());
		return null;
	}
	
	/**
	 * 检查资源是否已经存在(修改时)
	 * @return
	 * @throws Exception
	 */
	public String validateUpdateData()throws Exception{
		String roleName = new String(roleDto.getRole().getBytes("iso8859-1"),"gbk");
		System.out.println("***********************"+ roleDto.getId());
		System.out.println(roleName);
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter(); 
		StringBuffer validateMsg=null;
		if("".equals(roleName)){
			validateMsg = new StringBuffer("");
		}else{
			TbSysRole roleTest = roleService.findTbSysRoleByName(roleName,roleDto.getId());
			if(roleTest==null){	
				validateMsg = new StringBuffer("<font color='green'>ok</font>");		
			}else{
				validateMsg = new StringBuffer("<font color='red'> 角色名已存在</font>");			
			}
		}
		
		out.print(validateMsg.toString());
		return null;
	}

	public RoleServiceImpl getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleServiceImpl roleService) {
		this.roleService = roleService;
	}

	public RoleDTO getRoleDto() {
		return roleDto;
	}

	public void setRoleDto(RoleDTO roleDto) {
		this.roleDto = roleDto;
	}

	public List<RoleDTO> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<RoleDTO> roleList) {
		this.roleList = roleList;
	}

	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public RoleResourceServiceImpl getRoleResourceService() {
		return roleResourceService;
	}

	public void setRoleResourceService(RoleResourceServiceImpl roleResourceService) {
		this.roleResourceService = roleResourceService;
	}

	public List<TbSysRoleResource> getRoleResourceList() {
		return roleResourceList;
	}

	public void setRoleResourceList(List<TbSysRoleResource> roleResourceList) {
		this.roleResourceList = roleResourceList;
	}

	public ResourceServiceImpl getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceServiceImpl resourceService) {
		this.resourceService = resourceService;
	}

	public List<ResourceDTO> getResourceDtoList() {
		return resourceDtoList;
	}

	public void setResourceDtoList(List<ResourceDTO> resourceDtoList) {
		this.resourceDtoList = resourceDtoList;
	}

	public Integer getRoleResourceId() {
		return roleResourceId;
	}

	public void setRoleResourceId(Integer roleResourceId) {
		this.roleResourceId = roleResourceId;
	}

	public Integer getResourceId() {
		return resourceId;
	}

	public void setResourceId(Integer resourceId) {
		this.resourceId = resourceId;
	}

	public LogServiceImpl getLogService() {
		return logService;
	}

	public void setLogService(LogServiceImpl logService) {
		this.logService = logService;
	}

}
